package band.wukong.practice.javase.core.io.typicalUsage;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;

/**
 * Desc: 格式化的内存输入
 * Author: sunjs
 * Date: 2009-12-17 16:26:35
 */
public class FormattedMemoryInput {
//    static final String FILE_IN = "src/com/jsun/javase/corepackage/io/testdir/poem.txt";
    static final String FILE_IN = "src/com/jsun/javase/corepackage/io/testdir/en.txt";


    public static void main(String[] args) throws IOException {
        formattedMemoryInput();
//        testEOF();
    }

    public static void formattedMemoryInput() throws IOException {
        try {
            DataInputStream in = new DataInputStream(
                    new ByteArrayInputStream(
                            BufferedInputFile.read(FILE_IN).getBytes()));

            //使用readByte()一次一个字节地读取字符，那么任何字节的值都是合法的，返回值无法用来检查输入是否结束
            while(true) {
                System.out.print((char)in.readByte());//中文会乱码
            }

        } catch (EOFException e) {
            System.err.println("End of stream");
        }
    }

    public static void testEOF() throws IOException {
        DataInputStream in = new DataInputStream(
                    new ByteArrayInputStream(
                            BufferedInputFile.read(FILE_IN).getBytes()));

        //使用available()可以查看还有多少可供存取的字符
        while(in.available() != 0) {
            System.out.print((char)in.readByte());
        }
    }

}
